package com.ruoyi.products.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.suppliers.service.ISuppliersService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.products.domain.Products;
import com.ruoyi.products.service.IProductsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品功能Controller
 * 
 * @author ruoyi
 * @date 2024-09-24
 */
@RestController
@RequestMapping("/products/products")
public class ProductsController extends BaseController
{
    @Autowired
    private IProductsService productsService;
    @Autowired
    private ISuppliersService suppliersService;

    /**
     * 查询产品功能列表
     */
    @PreAuthorize("@ss.hasPermi('products:products:list')")
    @GetMapping("/list")
    public TableDataInfo list(Products products)
    {
        startPage();
        List<Products> list = productsService.selectProductsList(products);
        return getDataTable(list);
    }

    /**
     * 导出产品功能列表
     */
    @PreAuthorize("@ss.hasPermi('products:products:export')")
    @Log(title = "产品功能", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Products products)
    {
        List<Products> list = productsService.selectProductsList(products);
        ExcelUtil<Products> util = new ExcelUtil<Products>(Products.class);
        util.exportExcel(response, list, "产品功能数据");
    }

    /**
     * 获取产品功能详细信息
     */
    @PreAuthorize("@ss.hasPermi('products:products:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(productsService.selectProductsById(id));
    }

    /**
     * 新增产品功能
     */
    @PreAuthorize("@ss.hasPermi('products:products:add')")
    @Log(title = "产品功能", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Products products)
    {
      Integer id = suppliersService.selIdBySupplierName(products.getSupplierName());
      products.setSupplierId(id);
        return toAjax(productsService.insertProducts(products));
    }

    /**
     * 修改产品功能
     */
    @PreAuthorize("@ss.hasPermi('products:products:edit')")
    @Log(title = "产品功能", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Products products)
    {  Integer id = suppliersService.selIdBySupplierName(products.getSupplierName());
        products.setSupplierId(id);
        return toAjax(productsService.updateProducts(products));
    }

    /**
     * 删除产品功能
     */
    @PreAuthorize("@ss.hasPermi('products:products:remove')")
    @Log(title = "产品功能", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(productsService.deleteProductsByIds(ids));
    }
}
